#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "Control/Init.h"
#include "Control/LoginControl.h"
#include <future>
#include <QDebug>
#include <QObject>
#include "Tool/work.h"
#include "Tool/mythread.h"
#include "Control/ListControl.h"
#include "IM_CoreApi.h"

#ifdef WIN32||WIN64
#include <QFont>
#endif
int main(int argc, char *argv[])
{
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

  QGuiApplication app(argc, argv);
  qmlRegisterType<IM_GUI_KIT::LoginControl>("LoginControl",1,0,"LoginControl");
  qmlRegisterType<IM_GUI_KIT::ListControl>("ListControl",1,0,"ListControl");
#ifdef WIN32||WIN64
  QFont font;
  font.setFamily("微软雅黑");//设置全局字体
  app.setFont(font);
#endif

  QQmlApplicationEngine *engine= IM_GUI_KIT::Init::Instance()->getEngine();
  const QUrl urlmain(QStringLiteral("qrc:/main.qml"));
  const QUrl urllogin(QStringLiteral("qrc:/Login.qml"));
  QObject::connect(engine, &QQmlApplicationEngine::objectCreated,
                   &app, [urllogin](QObject *obj, const QUrl &objUrl) {
    if (!obj && urllogin == objUrl)
      QCoreApplication::exit(-1);
  }, Qt::QueuedConnection);
  engine->load(urllogin);
  engine->load(urlmain);
  engine->rootContext()->setContextProperty("myFriendLists",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getFriendList()));
  engine->rootContext()->setContextProperty("mySessionLists",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getSessionList()));

  engine->rootContext()->setContextProperty("currSessionList",QVariant::fromValue(nullptr));
  //  engine->rootContext()->setContextProperty("haveSession",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getSessionList().size()!=0));

  qDebug() << "Main thread : " << QThread::currentThreadId();

  return app.exec();
}
